<template>
  <dialog-editor ref="dialog" :width="50"></dialog-editor>
</template>

<script>
  import DialogEditor from '@/components/DialogEditor'
  import dialogMixins from '@/utils/commonDialogMixins'
  import {btnBlur} from '@/utils'
  import {mRequest, mRequestPromise} from '@/utils/request'

  export default {
    mixins: [dialogMixins],
    components: {DialogEditor},

    data() {
      return {
        deepCategoryMap: {},
        productInfoMap: {},
        dataFormConfig: {
          id: {
            hidden: true
          },
          consignNo: {
            label: '发货编号',
            disabled: true
          },
          deliveryTime: {
            label: '发货日期',
            type: 'date'
          },
          licencePlateId: {
            label: '车牌号',
            type: 'list',
            filterable: true,
            list: [],
          },
          taxInclusive: {
            label: '含运费',
            type: 'radio',
            radioBorder: false,
            radioList: [
              {label: 0, text: '不含税'},
              {label: 1, text: '含税'}
            ],
            default: 0
          },
          freight: {
            label: '运费',
            max: 10000000,
            default: 0
          },
          totalWeightInfo: {
            label: "重量",
            max: 10000000,
            default: 0
          },
          remark: {
            label: '备注',
            inputType: 'textarea',
            placeholder: '请输入备注'
          },
          tableListData: {
            type: 'table',
            data: [],
            column: {
              productNo: {
                label: '产品名称',
                width: 120
              },
              deliveryNumber: {
                label: '数量',
                width: 120
              },
              deliveryAddress: {
                label: '收货地址',
                width: 120
              },
              warehouseName:{
                label: '位置',
              },

              remark: {
                label: '备注',
                width: 120
              },

              totalPrice: {
                label: '产品总价',
                width: 120
              }
            },
            operate: {
              addOneLine: {
                label: '新增发货产品',
                icon: 'el-icon-edit',
                type: 'primary',
                clickHandler: this.addNewProductInfo
              },
              deleteOneLine: {
                label: '删除选中的产品',
                icon: 'el-icon-delete',
                type: 'danger',
                clickHandler: this.deleteProductInfo
              },
              editOneLine: {
                label: '修改选中的产品',
                icon: 'el-icon-edit-outline',
                type: 'success',
                clickHandler: this.editProductInfo
              }
            }
          }
        }
      }
    },
    methods: {
      async contractTimeChangedHandler(date) {
        const res = await mRequestPromise('contractApi/contractNoGetByContractTime', {contractTime: date})
        const contractNo = this.R.path(['data', 'contractNo'], res)

        if (!contractNo) {
          return
        }
        this.dataForm.contractNo = contractNo
      },
      editProductInfo(selection, evt) {
        const tmpItem = this.R.head(selection)

        if (this.R.isNil(tmpItem)) {
          return
        }
        const tmpItemIndex = this.dataForm.tableListData.indexOf(tmpItem)

        if (tmpItemIndex === -1) {
          return
        }
        const cb = (newValue) => {
          const tmpValue = this.productOrderHandler(newValue)

          this.dataForm.tableListData.splice(tmpItemIndex, 1, tmpValue)
          this.contractTotalPriceSet()
        }
        this.$parent.openConsignNoteDetailDialog(tmpItem, cb)
        btnBlur(evt)
      },
      productOrderHandler(newValue) {

        newValue.productNo = this.productInfoMap[newValue.productId]
        if (this.R.isNil(newValue.productNo)) {
          mRequest("productApi/productInfoInfo", {id: newValue.productId}, (res) => {
            const productInfo= this.R.pathOr([], ['data', 'productInfo'], res)
            if (productInfo) {
              newValue.productNo = productInfo.productNo
              this.productInfoMap[newValue.productId] = newValue.productNo
            }
          })
        }

        return newValue
      },
      deleteProductInfo(selection, evt) {
        const selectionIndex = selection.map(item => this.dataForm.tableListData.indexOf(item)).sort((a, b) => {
          return a - b;
        })

        for (let i = selectionIndex.length - 1; i >= 0; i--) {
          this.dataForm.tableListData.splice(selectionIndex[i], 1)
        }
        this.contractTotalPriceSet()
        btnBlur(evt)
      },
      addNewProductInfo(_, evt) {
        const cb = (newValue) => {
          const tmpValue = this.productOrderHandler(newValue)

          this.dataForm.tableListData.push(tmpValue)
          this.contractTotalPriceSet()
        }
        this.$parent.openConsignNoteDetailDialog({}, cb)
        btnBlur(evt)
      },
      contractTotalPriceSet() {
        const orderPriceList = this.dataForm.tableListData.map(window.R.propOr(0, 'weightInfo'))

        this.dataForm.totalAmount = window.R.reduce(window.R.add, 0, orderPriceList)
      },
      addNewClientInfo() {
        const cb = (newValue) => {
          this.initClientInfoList()
          this.dataForm.clientId = newValue.id
        }
        this.$parent.openClientInfoDialog(cb)
      },
      deepCategoryMapInit() {
        mRequest('deepApi/deepCategoryList', {}, (res) => {
          const tmpList = this.R.pathOr([], ['data', 'list'], res)

          tmpList.forEach(item => {
            const {id, name} = item

            this.deepCategoryMap[id] = name
          })
        })
      },
      productInfoMapInit() {
        mRequest('productApi/getProductInfoList', {}, (res) => {
          const productInfoList = this.R.pathOr([], ['data', 'list'], res)
          productInfoList.forEach(item => {
            const {id, name} = item
            this.productInfoMap[id] = name
          })

        })
      },
      init(id) {
        this.dataForm.id = id || 0
        this.productInfoMapInit()
        this.initLicencePlateInfoList()
        this.$refs.dialog.initDialog({
          dataForm: this.dataForm,
          config: this.dataFormConfig,
          dataRule: this.getNeedDataRule('contractTime', 'clientId', 'deliveryTime', 'status'),
          initApi: 'consignApi/consignNoteInfo',
          saveApi: 'consignApi/saveConsignNoteDetail',
          updateApi: 'consignApi/updateConsignNoteDetail',
          objectName: 'consignNote',
        })
      }
    }
  }
</script>
